package fr.univ.lille1.android.network;

import java.util.List;

import retrofit.RestAdapter;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.PUT;
import retrofit.http.Path;
import fr.univ.lille1.android.domain.Message;

/**
 * Manager of the message resource to make CRUD requests on the server.
 * 
 * @author Gerard
 */
public class MessageManager {
	/**
	 * Attribute of the MessageRequest interface to send request on the server.
	 * This attribute is initialized by the library retrofit.
	 */
	private MassageRequest mMessageRequests;

	/**
	 * Constructor of the MessageManager to initialize the attribute
	 * MassageRequest.
	 * 
	 * @param restAdapter
	 *            Rest adapter of the library retrofit
	 */
	public MessageManager(final RestAdapter restAdapter) {
		mMessageRequests = restAdapter.create(MassageRequest.class);
	}

	/**
	 * Request on the server to retrieve a message with his id.
	 * 
	 * @param id
	 *            Identifier of the message
	 * @return Message object
	 */
	public Message findById(int id) {
		return mMessageRequests.findById(id);
	}

	/**
	 * Request on the server to retrieve messages of a location.
	 * 
	 * @param latitude
	 *            Latitude of the user
	 * @param longitude
	 *            Longitude of the user
	 * @param distance
	 *            Distance in meter
	 * @return List of message object
	 */
	public List<Message> findByLocation(float latitude, float longitude,
			float distance) {
		return mMessageRequests.findByLocation(latitude, longitude, distance);
	}

	/**
	 * Request on the server to create a message user on the system.
	 * 
	 * @param userId
	 *            Identifier of the user
	 * @param title
	 *            Title of the message
	 * @param content
	 *            Content of the message
	 * @param gpsLat
	 *            Latitude of the user
	 * @param gpsLong
	 *            Longitude of the user
	 * @return Message object
	 */
	public Message create(int userId, String title, String content,
			float gpsLat, float gpsLong) {
		return mMessageRequests.create(userId, title, content, gpsLat, gpsLong);
	}

	/**
	 * Request on the server to update a message of the system.
	 * 
	 * @param id
	 *            Identifier of the message
	 * @param title
	 *            Title of the message
	 * @param content
	 *            Content of the message
	 * @return Message object
	 */
	public Message update(int id, String title, String content) {
		return mMessageRequests.update(id, title, content);
	}

	/**
	 * Request on the server to like a message.
	 * 
	 * @param id
	 *            Identifier of the message
	 * @param userId
	 *            Identifier of the user
	 * @return Message object
	 */
	public Message like(int id, int userId) {
		return mMessageRequests.like(id, userId);
	}

	/**
	 * Request on the server to dislike a message.
	 * 
	 * @param id
	 *            Identifier of the message
	 * @param userId
	 *            Identifier of the user
	 * @return Message object
	 */
	public Message dislike(int id, int userId) {
		return mMessageRequests.dislike(id, userId);
	}

	/**
	 * Request on the server to alert a message.
	 * 
	 * @param id
	 *            Identifier of the message
	 * @param userId
	 *            Identifier of the user
	 * @return Message object
	 */
	public Message alert(int id, int userId) {
		return mMessageRequests.alert(id, userId);
	}

	/**
	 * Interface to match some methods with API urls of the server. The server
	 * allow any clients to make CRUD request on Messages resource.
	 * 
	 * @author Gerard
	 */
	interface MassageRequest {
		@GET("/message/{id}")
		Message findById(@Path("id") int id);

		@GET("/message/latitude/{latitude}/longitude/{longitude}/distance/{distance}")
		List<Message> findByLocation(@Path("latitude") float latitude,
				@Path("longitude") float longitude,
				@Path("distance") float distance);

		@FormUrlEncoded
		@PUT("/message")
		Message create(@Field("userId") int userId,
				@Field("title") String title, @Field("content") String content,
				@Field("gpsLat") float gpsLat, @Field("gpsLong") float gpsLong);

		@FormUrlEncoded
		@PUT("/message")
		Message update(@Field("userId") int id, @Field("title") String title,
				@Field("content") String content);

		@FormUrlEncoded
		@PUT("/message/{id}/like")
		Message like(@Path("id") int id, @Field("userId") int userId);

		@FormUrlEncoded
		@PUT("/message/{id}/dislike")
		Message dislike(@Path("id") int id, @Field("userId") int userId);

		@FormUrlEncoded
		@PUT("/message/{id}/alert")
		Message alert(@Path("id") int id, @Field("userId") int userId);
	}
}
